package com.jinbei.task;

import com.jinbei.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author zflu
 * 系统启动时执行查询被禁用的用户
 * 2018年7月9日13:46:00
 * Created by lucky on 2018/7/9.
 */
@Component
@Order(1)
public class CacheRunner implements CommandLineRunner {

    private Logger logger = LoggerFactory.getLogger(CacheRunner.class);

    /**
     * 缓存用户 user_id
     */
    public static Map<String,Integer> cacheMap = new ConcurrentHashMap();

    @Autowired
    private UserService userService;

    @Override
    public void run(String... strings) throws Exception {
        logger.info("-------init cacheMap start-------");
        try {
            Map lockUser = userService.queryUserByLock();
            if(lockUser != null){
                cacheMap.putAll(lockUser);
            }
        }catch (Exception e){
            logger.error("初始化 cacheMap 失败 error: {}",e);
        }
        logger.info("-------init cacheMap end-------");
    }
}
